const cacheHelper = require('../helper/cache_helper.js');
const pageHelper = require('../helper/page_helper.js');
const cloudHelper = require('../helper/cloud_helper.js');
const timeHelper = require('../helper/time_helper.js');
const PassortBiz = require('../biz/passport_biz.js');
const setting = require('../setting/setting.js');

module.exports = Behavior({
	data: {
    myTodayList: null,
    
	},

	methods: {
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: async function (options) {
      if (setting.IS_SUB) wx.hideHomeButton();
      if(options.q){
        const q = decodeURIComponent(options.q) // 获取到二维码原始链接内容
        const scancode_time = parseInt(options.scancode_time) // 获取用户扫码时间 
        this.setData({
          scanParam:q,
          scanTime:scancode_time,
        })
      }
		},

		_loadTodayList: async function () {
			try {
        let data = this.data;
				let params = {
          day: timeHelper.time('Y-M-D'),
          scanParam:data.scanParam,
          scanTime:data.scanTime,
				}
				let opts = {
					title: 'bar'
				}
				await cloudHelper.callCloudSumbit('my/my_join_someday', params, opts).then(res => {
					this.setData({
						myTodayList: res.data
					});
        });
			} catch (err) {
				console.log(err)
			}
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function () {
      if(this.data.scanTime&&this.data.user.USER_CUSTOMER_TYPE<1){
        
      }
    },

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: async function () {
      await this._loadLatestUserSignLog();
			await this._loadTodayList();
      await this._loadUser();
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function () {

		},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function () {

		},
    _loadLatestUserSignLog:async function(e){
    try{
      let opts = {
				title: 'bar'
			}
      await cloudHelper.callCloudSumbit('my/my_join_latestsignlog',{},opts).then(res => {
        this.setData({
          signLog: res.data
        });
      });
    } catch (err) {
      console.log(err)
    }
    },
		_loadUser: async function (e) {

			let opts = {
				title: 'bar'
			}
      let user = await cloudHelper.callCloudData('passport/my_detail', {}, opts);
			if (!user) return;
      let opts1 = {
        title: 'bar'
      }
      // let about = await cloudHelper.callCloudData('home/setup_all', {}, opts1);

      // if (!about) {
      //   return;
      // }
			this.setData({
        user
			})
		},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: async function () {

      // await this._loadLatestUserSignLog();
			// await this._loadTodayList();
      // await this._loadUser();
			wx.stopPullDownRefresh();
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function () {

		},


		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function () {
      return {
        title: '赶紧来免费享受多次艾灸调理吧,地点在四正街东侧乐悠悠生活服务馆(爱尚佳布店南对面)',
        imageUrl: 'https://cloud1-6gyjpsnqca2b4f98-1324647503.tcloudbaseapp.com/share.png',
        path: 'projects/A00/meet/detail/meet_detail?id=701d5ae7666f6f7b030551420a75d242',
      }
    },
		url: function (e) {
			pageHelper.url(e, this);
		},

		setTap: function (e, skin) {
			let itemList = ['清除缓存', '后台管理'];
			wx.showActionSheet({
				itemList,
				success: async res => {
					let idx = res.tapIndex;
					if (idx == 0) {
						cacheHelper.clear();
						pageHelper.showNoneToast('清除缓存成功');
					}

					if (idx == 1) {
						pageHelper.setSkin(skin);
						if (setting.IS_SUB) {
							PassortBiz.adminLogin('admin', '123456', this);
						} else {
							wx.reLaunch({
								url: '/pages/admin/index/login/admin_login',
							});
						}

					}

				},
				fail: function (res) {}
			})
		}
	}
})